package lnu.project.cart.content;

import java.util.HashMap;

import lnu.project.cart.entity.Basket;
import lnu.project.cart.entity.Product;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

public class BasketContentProvider extends ContentProvider {
	public static final String DB_BASE = "lnuShoppingDB.db";

	public static final Uri CONTENT_BASKET_URI = Uri
			.parse("content://com.lnu.project.cart/baskets");
	public static final Uri CONTENT_PRODUCT_URI = Uri
			.parse("content://com.lnu.project.cart/products");

	public static final int BASKET_URI_CODE = 1;
	public static final int BASKET_URI_CODE_ID = 2;

	public static final int PRODUCT_URI_CODE = 3;
	public static final int PRODUCT_URI_CODE_ID = 4;

	private static final UriMatcher mUriMatcher;
	private static HashMap<String, String> basketContactMap;
	private static HashMap<String, String> productContactMap;

	private SQLiteDatabase dbBasket;
	private SQLiteDatabase dbProduct;

	static {
		mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		mUriMatcher.addURI("com.lnu.project.cart",
				BasketDatabaseHelper.DATABASE_TABLE, BASKET_URI_CODE);
		mUriMatcher.addURI("com.lnu.project.cart",
				BasketDatabaseHelper.DATABASE_TABLE + "/#", BASKET_URI_CODE_ID);

		basketContactMap = new HashMap<String, String>();
		basketContactMap.put(Basket.ID, Basket.ID);
		basketContactMap.put(Basket.NAME, Basket.NAME);

		mUriMatcher.addURI("com.lnu.project.cart",
				ProductDatabaseHelper.DATABASE_TABLE, PRODUCT_URI_CODE);
		mUriMatcher.addURI("com.lnu.project.cart",
				ProductDatabaseHelper.DATABASE_TABLE + "/#",
				PRODUCT_URI_CODE_ID);

		productContactMap = new HashMap<String, String>();
		productContactMap.put(Product.ID, Product.ID);
		productContactMap.put(Product.BASKET_ID, Product.BASKET_ID);
		productContactMap.put(Product.TYPE_OF_PRODUCT, Product.TYPE_OF_PRODUCT);
		productContactMap.put(Product.NAME, Product.NAME);
		productContactMap.put(Product.BARCODE, Product.BARCODE);
		productContactMap.put(Product.QUANTITY, Product.QUANTITY);
		productContactMap.put(Product.WEIGHT, Product.WEIGHT);
		productContactMap.put(Product.PRICE, Product.PRICE);
	}

	public String getDbName() {
		return (DB_BASE);
	}

	@Override
	public int delete(Uri url, String where, String[] whereArgs) {
		int retVal;
		switch (mUriMatcher.match(url)) {
		case BASKET_URI_CODE:
			retVal = dbBasket.delete(BasketDatabaseHelper.DATABASE_TABLE,
					where, whereArgs);
			break;
		case PRODUCT_URI_CODE:
			retVal = dbProduct.delete(ProductDatabaseHelper.DATABASE_TABLE,
					where, whereArgs);
			break;
		default:
			throw new SQLException("Failed to insert row into " + url);
		}
		getContext().getContentResolver().notifyChange(url, null);
		return retVal;
	}

	@Override
	public String getType(Uri arg0) {
		return null;
	}

	@Override
	public Uri insert(Uri url, ContentValues inValues) {

		ContentValues values = new ContentValues(inValues);
		long rowId;
		// Uri for return
		Uri uri;

		switch (mUriMatcher.match(url)) {
		case BASKET_URI_CODE:
			rowId = dbBasket.insert(BasketDatabaseHelper.DATABASE_TABLE, null,
					values);
			if (rowId > 0) {
				uri = ContentUris.withAppendedId(CONTENT_BASKET_URI, rowId);
			} else {
				throw new SQLException("Failed to insert row into " + url);
			}
			break;
		case PRODUCT_URI_CODE:
			rowId = dbProduct.insert(ProductDatabaseHelper.DATABASE_TABLE,
					null, values);
			if (rowId > 0) {
				uri = ContentUris.withAppendedId(CONTENT_PRODUCT_URI, rowId);
			} else {
				throw new SQLException("Failed to insert row into " + url);
			}
			break;
		default:
			throw new SQLException("Failed to insert row into " + url);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return uri;
	}

	@Override
	public boolean onCreate() {
		dbBasket = (new BasketDatabaseHelper(getContext()))
				.getWritableDatabase();
		dbProduct = (new ProductDatabaseHelper(getContext()))
				.getWritableDatabase();
		return (dbBasket == null || dbProduct == null) ? false : true;
	}

	@Override
	public Cursor query(Uri url, String[] projection, String selection,
			String[] selectionArgs, String sort) {
		Cursor c;
		String orderBy;
		switch (mUriMatcher.match(url)) {
		case BASKET_URI_CODE:
			if (TextUtils.isEmpty(sort)) {
				orderBy = Basket.NAME;
			} else {
				orderBy = sort;
			}
			c = dbBasket.query(BasketDatabaseHelper.DATABASE_TABLE, projection,
					selection, selectionArgs, null, null, orderBy);
			c.setNotificationUri(getContext().getContentResolver(), url);
			break;
		case PRODUCT_URI_CODE:
			if (TextUtils.isEmpty(sort)) {
				orderBy = Product.NAME;
			} else {
				orderBy = sort;
			}
			c = dbProduct.query(ProductDatabaseHelper.DATABASE_TABLE,
					projection, selection, selectionArgs, null, null, orderBy);
			c.setNotificationUri(getContext().getContentResolver(), url);
			break;
		default:
			throw new SQLException("Failed to insert row into " + url);
		}
		return c;
	}

	@Override
	public int update(Uri url, ContentValues values, String where,
			String[] whereArgs) {
		int retVal;
		switch (mUriMatcher.match(url)) {
		case BASKET_URI_CODE:
			retVal = dbBasket.update(BasketDatabaseHelper.DATABASE_TABLE,
					values, where, whereArgs);
			break;
		case PRODUCT_URI_CODE:
			retVal = dbProduct.update(ProductDatabaseHelper.DATABASE_TABLE,
					values, where, whereArgs);
			break;
		default:
			throw new SQLException("Failed to insert row into " + url);
		}
		getContext().getContentResolver().notifyChange(url, null);
		return retVal;
	}
}
